package com.woniuxy.business.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfiguration {
    //抢券消息队列
    @Bean
    public Queue killQueue(){
        //自定义过期时间、死信交换机、死信路由
        Map<String,Object> args=new HashMap<>();
        //指定死信交换机
        args.put("x-dead-letter-exchange","fail_exchange");
        //指定死信路由
        args.put("x-dead-letter-routing-key","fail");
        //设置消息队列最大容量
        args.put("x-max-length",100);
        //设置消息溢出后成为死信
        args.put("x-overflow","drop-head");

        return new Queue("kill_queue",true,false,false,args);
    }
    @Bean
    public DirectExchange killExchange(){
        return new DirectExchange("kill_exchange");
    }
    @Bean
    public Binding bindingKillQueueAndKillExchange(Queue killQueue, DirectExchange killExchange){
        return BindingBuilder.bind(killQueue).to(killExchange).with("kill");
    }

    //失败消息队列
    @Bean
    public Queue failQueue(){
        return new Queue("fail_queue");
    }
    @Bean
    public DirectExchange failExchange(){
        return new DirectExchange("fail_exchange");
    }
    @Bean
    public Binding bindingFailQueueAndFailExchange(Queue failQueue,DirectExchange failExchange){
        return BindingBuilder.bind(failQueue).to(failExchange).with("fail");
    }

    //数据库消息队列
    @Bean
    public Queue sqlQueue(){
        return new Queue("sql_queue");
    }
    @Bean
    public DirectExchange sqlExchange(){
        return new DirectExchange("sql_exchange");
    }
    @Bean
    public Binding bindingSqlQueueAndSqlExchange(Queue sqlQueue,DirectExchange sqlExchange){
        return BindingBuilder.bind(sqlQueue).to(sqlExchange).with("sql");
    }

    //发送信息的消息队列
    @Bean
    public Queue smsQueue(){
        return new Queue("sms_queue");
    }
    @Bean
    public Queue emailQueue(){
        return new Queue("email_queue");
    }
    @Bean
    public Queue newsQueue(){
        return new Queue("news_queue");
    }
    @Bean
    public DirectExchange messageExchange(){
        return new DirectExchange("message_exchange");
    }
    @Bean
    public Binding bindingSmsQueueAndMessageExchange(Queue smsQueue,DirectExchange messageExchange){
        return BindingBuilder.bind(smsQueue).to(messageExchange).with("sms");
    }
    @Bean
    public Binding bindingEmailQueueAndMessageExchange(Queue emailQueue,DirectExchange messageExchange){
        return BindingBuilder.bind(emailQueue).to(messageExchange).with("email");
    }
    @Bean
    public Binding bindingNewsQueueAndMessageExchange(Queue newsQueue,DirectExchange messageExchange){
        return BindingBuilder.bind(newsQueue).to(messageExchange).with("news");
    }
}